package mammuthus.web.service

import java.util
import java.util.Date
import mammuthus.web.bean.TestConfBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.jdbc.core.{BeanPropertyRowMapper, JdbcTemplate}
import org.springframework.stereotype.Service

import scala.collection.JavaConversions._
import net.csdn.common.collections.WowCollections
import org.squeryl.PrimitiveTypeMode
import mammuthus.web.db.{TTestConf, DB}

import scala.Tuple2
import scala.Some

/**
 * Created by fengbingjian on 15/5/10 16:08.
 */
@Service
class TestConfService extends PrimitiveTypeMode {

  private var jdbcTemplate: JdbcTemplate = null

  @Autowired
  def setJdbcTemplate(jdbcTemplate: JdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate
  }

  def addConf(name: String, url: String, host: String, user: String) = {
    TTestConf.find(name) match {
      case Some(i) => (true, null)
      case None =>
        transaction {
          DB.tTestConf.insert(new TTestConf(name, user,url, host))
        }
        (false, name + "已经存在")
    }
  }

  def updateConf(name: String, url: String, host: String, user: String) = {
    TTestConf.updateConf(new TTestConf(name, user, url, host))
  }


  def getUrl(name: String) = {

    val r = jdbcTemplate.queryForList("select url,user from t_test_conf where name = ?", name.replace("test-", ""))
    if(r != null && r.size() == 1) r.get(0).getOrElse("url", "")
    else ""
  }



  def getTTestConf(name: String) = {
     TTestConf.find(name)
  }

  def list(): util.List[TestConfBean] = {

    jdbcTemplate.query("select * from t_test_conf order by id desc", new BeanPropertyRowMapper[TestConfBean](classOf[TestConfBean]))

  }
}
